//
//  PeoplePickerDelegate.m
//  iWeighMyself
//
//  Created by Francisco Gindre on 8/4/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "PeoplePickerDelegate.h"



@implementation PeoplePickerDelegate
@synthesize appDelegate;
@synthesize newUserView;

//-(id)initWithNewUserViewController:(NewUserViewController*)view {
//    
//    self =[super init];
//    
//    newUserView = view;
//    [newUserView retain];
//    return self;
//    
//}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    
	UIAlertView *alert;
	switch(ABRecordGetRecordType(person))	
    {
        case kABPersonType:
            
            return YES;
            break;
            
                   
        case kABGroupType:
			/*
			 Tell the user that only person contacts are supported
			 */
			
			alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"GroupContactSelectedAlertTitle",@"")
															message:NSLocalizedString (@"@GroupContactSelectedAlertDescription",@"")
															delegate:nil
															cancelButtonTitle:NSLocalizedString(@"AlertCloseButtonTitle",@"")
															otherButtonTitles:nil];
			
			[alert show];
			[alert release];
			
		
            return NO;
            break;
            
    }

   
    
	return NO; //shouldn't get here
}

// don't continue after selecting property
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    
	self.appDelegate = (IWeighMyselfAppDelegate*)[[UIApplication sharedApplication] delegate];
    
    if(property!=kABPersonEmailProperty){
        
        NSLog(@"not an email");
    }
    else {
        
        
        /*
        get name
        */
        
        NSString *name= (NSString*)ABRecordCopyCompositeName(person);
        
        /*
         get email multi value array
         */
        
        ABMultiValueRef emailList = ABRecordCopyValue(person, property);
        
        /*
         get the selected record
         */
        NSString *email= (NSString*) ABMultiValueCopyValueAtIndex(emailList, identifier);
        
        
        NSMutableDictionary *retrievedContact = [NSMutableDictionary dictionaryWithObjectsAndKeys:name,@"DefaultContactName"
                                          ,email,@"DefaultContactEmail",nil];
        
        //newUserView.defaultContactDictionary =retrievedContact;
		
	//	[newUserView.defaultContactDictionary retain];
		
		NSLog(@"retrieved contact %@", retrievedContact);
		
		[retrievedContact retain];
        [self.appDelegate.plistXML setObject:retrievedContact forKey:@"DefaultContact"];
        
		NSLog(@"retrieved contact in plistxml is %@",self.appDelegate.plistXML);
		/*
		
        [(NSMutableArray*)[newUserView.cellValues objectAtIndex:newUserView.defaultContactCellIndexPath.section] replaceObjectAtIndex:newUserView.defaultContactCellIndexPath.row withObject: name];
        
        /*
		 free some memory
		 */
		
        [email release];
        [name release];
		[retrievedContact release];
		
		        
		/*
		 dismiss the picker. 
		 */
		
        [peoplePicker dismissModalViewControllerAnimated:YES];
		
		/*
		 update the cell values with the that we just gathered
		 */
		
       // [newUserView updateCellValues];
		

    }
    
    	return NO;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [peoplePicker dismissModalViewControllerAnimated:YES];    
  
}
@end
